﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Customer_Profile.eCardReport.DBDao;
using KVB.Common;
using System.Text;
using Customer_Profile.eCard.DBDao;
using DevExpress.Data;
using DevExpress.Web.ASPxGridView;


namespace Customer_Profile.eCardReport
{
    public partial class RankingDealer_Report : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet dsProd = ProductDao.getAllProduct("ALL");
            DevExpress.Web.ASPxEditors.ASPxListBox listbox = (DevExpress.Web.ASPxEditors.ASPxListBox)ASPxDropDownEdit1.FindControl("listBox");
            listbox.DataSource = dsProd;
            listbox.TextField = "procduct_name";
            listbox.ValueField = "procduct_code";
            listbox.DataBind();

            DevExpress.Web.ASPxEditors.ListEditItem li = new DevExpress.Web.ASPxEditors.ListEditItem("选择全部");
            listbox.Items.Insert(0, li);          
            if (!Page.IsPostBack)
            {
                //DataSet dsProd = ProductDao.getAllProduct("ALL");
                //dropProduct.DataSource = dsProd;
                //dropProduct.DataTextField = "procduct_name";
                //dropProduct.DataValueField = "procduct_code";
                //dropProduct.DataBind();
                //ListItem liAll = new ListItem();
                //liAll.Text = "All";
                //liAll.Value = "All";
                //dropProduct.Items.Insert(0, liAll);

                txtgedatebegin.Text = DateTime.Now.ToString("yyyy-MM-01");
                txtgedateend.Text = DateTime.Now.ToString("yyyy-MM-dd");
                Comm.BindDll(ref dateType, "dateType", false, "");

                //string strProdCode = "";
                //for (int i = 0; i < dsProd.Tables[0].Rows.Count; i++)
                //{
                //    strProdCode += dsProd.Tables[0].Rows[i]["procduct_code"].ToString() + ";";
                //}
                //strProdCode = strProdCode.Substring(0, strProdCode.Length - 1);
                //ASPxDropDownEdit1.Text = strProdCode;
            }
            BindDataView();
            ASPxGridView1.GroupSummarySortInfo.Clear();
            ColumnSortOrder sortOrder = ColumnSortOrder.Descending;
            ASPxGridView1.GroupSummarySortInfo.AddRange(new ASPxGroupSummarySortInfo("dealer", ASPxGridView1.GroupSummary["initial_amount"], sortOrder));

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private string GetWhere()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" where status='PASS' and if_returncard='N' ");
                //where value_date between '2012-8-01' and GETDATE() and prod_code='S91'
                if (txtgedatebegin.Text != "" && txtgedateend.Text != "")
                {
                    if (dateType.SelectedItem.Value.Trim().Equals("review_date"))
                    {
                        sb.Append(" AND convert(date,review_date) between '" + txtgedatebegin.Text.Trim() + "' and '" + txtgedateend.Text.Trim() + "' ");
                    }
                    else if (dateType.SelectedItem.Value.Trim().Equals("value_date"))
                    {
                        sb.Append(" AND convert(date,value_date) between '" + txtgedatebegin.Text.Trim() + "' and '" + txtgedateend.Text.Trim() + "' ");
                    }
                }
                //if (dropProduct.SelectedIndex > 0)
                //{
                //    sb.Append(" And prod_code='" + dropProduct.SelectedItem.Value.Trim() + "'");
                //}

                if (!ASPxDropDownEdit1.Text.Trim().Equals(""))
                {
                    sb.Append(" And prod_code in (select STR1 from dbo.fn_SPLITSTR('" + ASPxDropDownEdit1.Text.Trim() + "',';')) ");
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("rankinglist_report_GetWhere", ex.ToString());
                return "";
            }
        }

        private void BindDataView()
        {
            string sWhere = this.GetWhere();
            try
            {
                DataSet ds = ReportDao.GetRankingByEntityReport(sWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ASPxGridView1.DataSource = ds.Tables[0];
                    ASPxGridView1.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("EntityRanking_BindDataView", ex.ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteXlsToResponse();
        }
    }
}